package com.imc.api.notice;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.imc.core.config.feign.MyPageImpl;
import com.imc.platform.base.notice.dao.INoticeMapper;
import com.imc.platform.base.notice.pojo.Notice;
import com.imc.platform.base.notice.pojo.NoticeRet;
import com.imc.platform.base.notice.service.INoticeService;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 方法描述:小程序公告接口
 * SJC 2018-9-10 11:26:15
 */
@Slf4j
@RequestMapping("/api/public")
@RestController
public class NoticeApiController {

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @Autowired
    INoticeService iNoticeService;//公告服务类

    @Autowired
    INoticeMapper iNoticeMapper;//公告mapper

    /**
     * 方法描述:根据类型查询公告列表
     * SJC 2018-9-11 16:22:59
     */
    @RequestMapping("/notice/queryNoticeList")
    public ResultDTO queryNoticeList(@RequestParam String noticeType) throws Exception {
        JSONObject map = new JSONObject();
        List <Map <String, Object>> retData = iNoticeService.queryNoticeList(noticeType);
        map.put("data", retData);
        ResultDTO obj = ResultDTO.builder().data(map).build();
        return obj;
    }

    /*
     * YT  2018/10/28 21:07
     * 方法描述：查询公告信息
     */
    @RequestMapping("/notice/queryNoticeInfo")
    public ResultDTO queryNoticeInfo(@RequestParam String noticeId) throws Exception {
        JSONObject map = new JSONObject();
        Notice notice = iNoticeService.queryNoticeById(noticeId);
        map.put("data", notice);
        ResultDTO obj = ResultDTO.builder().data(map).build();
        return obj;
    }

    /*
     * YT  2018/10/28 21:06
     * 方法描述：页面顶部公告
     */
    @GetMapping("/topNotice")
    ResultDTO topNotice() {
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    Notice notice = iNoticeMapper.queryTopNotice();
                    if(!"".equals(notice.getContent())){
                        notice.setContent(notice.getContent());
                    }
                    t.setData(notice);
                },
                (e, t) -> {
                    t.setIsSuccess(false);
                    t.setMessage("获取公告失败");
                }
        );
    }

    @GetMapping("/wx/queryNoticeListByTypeAndId")
    @ResponseBody
    public ResultDTO queryNoticeListByTypeAndId(@RequestParam String noticeType, @RequestParam(defaultValue = "") String noticeId, @PageableDefault Pageable pageable){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    Map<String,Object>queryParms=new HashMap<>();
                    queryParms.put("type",noticeType);
                    queryParms.put("id",noticeId);
                    queryParms.put("size",pageable.getPageSize());
                    queryParms.put("number",pageable.getPageNumber());
//                    Notice notice = iNoticeMapper.queryTopNotice();
                    PageInfo info = new PageInfo <>(iNoticeService.queryNoticeListByTypeAndId(queryParms));
                    t.setData(new MyPageImpl(info.getList(), info.isIsLastPage()));
                    t.setIsSuccess(true);
                    t.setMessage("查询成功");
                },
                (e, t) -> {
                    t.setCode("201");
                    t.setIsSuccess(false);
                    t.setMessage("获取公告失败");
                }
        );
    }

    @GetMapping("/wx/queryNoticeDetByTypeAndId")
    @ResponseBody
    public ResultDTO queryNoticeDetByTypeAndId(@RequestParam String noticeType,@RequestParam(defaultValue = "") String noticeId){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    Map<String,Object>queryParms=new HashMap<>();
                    queryParms.put("type",noticeType);
                    queryParms.put("id",noticeId);
                    t.setData(iNoticeService.queryNoticeDetByTypeAndId(queryParms));
                    t.setIsSuccess(true);
                    t.setMessage("查询成功");
                },
                (e, t) -> {
                    t.setCode("201");
                    t.setIsSuccess(false);
                    t.setMessage("获取公告失败");
                }
        );
    }

    @GetMapping("/wx/queryArticleList")
    @ResponseBody
    public ResultDTO queryArticleList(@RequestParam(defaultValue = "") String noticeType, @RequestParam(defaultValue = "") String noticeId, @PageableDefault Pageable pageable){
        return iComboExecptionForTFunc.executeFlow(
                ResultDTO.builder().build(),
                t -> {
                    Map<String,Object>queryParms=new HashMap<>();
                    queryParms.put("id",noticeId);
                    queryParms.put("size",pageable.getPageSize());
                    queryParms.put("number",pageable.getPageNumber());
                    if(StringUtils.isNotEmpty(noticeType)){
                        queryParms.put("type",noticeType);
                        if("29".equals(noticeType) || "30".equals(noticeType)){
                            t.setData(iNoticeService.queryOrderAndLeave(queryParms));
                        }else {
                            PageInfo info = new PageInfo <>(iNoticeService.queryNoticeListByTypeAndId(queryParms));
                            t.setData(new MyPageImpl(info.getList(), info.isIsLastPage()));
                        }

                    }else {
                        String [] strCode={"5","6"};
                        String [] strNmae={"桃李新闻","精美文选"};
                        queryParms.put("size","3");
                        queryParms.put("number","0");
                        List<NoticeRet>retList=new ArrayList<>();
                        for(int i=0; i<strCode.length;i++){
                            NoticeRet noticeRet=new NoticeRet();
                            queryParms.put("type",strCode[i]);
                            PageInfo info = new PageInfo <>(iNoticeService.queryNoticeListByTypeAndId(queryParms));
                            noticeRet.setTitle(strNmae[i]);
                            noticeRet.setCode(strCode[i]);
                            noticeRet.setChild(info.getList());
                            retList.add(noticeRet);
                        }
                        t.setData(retList);
                    }
                    t.setIsSuccess(true);
                    t.setMessage("查询成功");
                },
                (e, t) -> {
                    t.setCode("201");
                    t.setIsSuccess(false);
                    t.setMessage("获取公告失败");
                }
        );
    }
}